package controllers

import (
	"WebSvr/modules/Echart"
	"WebSvr/modules/common"
	"WebSvr/modules/mysql"

	"net/http"
	"strconv"
	"text/template"
	"time"

	"github.com/astaxie/beego"
)

type ReportController struct {
	beego.Controller
}

//转到Report页面
func (this *ReportController) GetreportWeb() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			fallthrough
		case common.ACCESS_REPORT:
			{
				this.Data["xsrf_token"] = this.XSRFToken()
				this.Data["adminName"] = userName
				RecordUserAction(userName, accessInt, ip, "GetreportWeb成功")
				this.TplName = "echartReport.html"
			}
		default:
			{
				RecordUserAction(userName, accessInt, ip, "GetreportWeb失败,无此用户")
				this.CustomAbort(http.StatusUnauthorized, "GetreportWeb失败,无此用户")
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

func (this *ReportController) GetServerName() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		nameSlice := webAdmin.GetAllServerName()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			fallthrough
		case common.ACCESS_REPORT:
			{
				RecordUserAction(userName, accessInt, ip, "GetServerName成功")
				this.Data["json"] = nameSlice
				this.ServeJSON()
			}
		default:
			{
				RecordUserAction(userName, accessInt, ip, "GetServerName失败,无此用户")
				this.CustomAbort(http.StatusUnauthorized, "GetServerName失败,无此用户")
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

func (this *ReportController) escapeC2SString() common.C2SJson {
	selTypeC2S := this.Input().Get("selType")
	serverNameC2S := this.Input().Get("serverName")
	startTmC2S := this.Input().Get("startTime")
	endTmC2S := this.Input().Get("endTime")
	/*转换预防XSS*/
	selTypeEscape := template.JSEscapeString(selTypeC2S)
	serverNameEscape := template.JSEscapeString(serverNameC2S)
	startTmEscape := template.JSEscapeString(startTmC2S)
	endTmEscape := template.JSEscapeString(endTmC2S)

	var msgC2SJson common.C2SJson
	selTypeInt, _ := strconv.Atoi(selTypeEscape)
	msgC2SJson.SelType = int32(selTypeInt)
	msgC2SJson.ServerName = serverNameEscape
	msgC2SJson.StartTime = startTmEscape
	msgC2SJson.EndTime = endTmEscape
	return msgC2SJson
}

func (this *ReportController) checkMsgC2SJson(msgC2SJson common.C2SJson) bool {
	st, stErr := time.Parse(common.TimeModel, msgC2SJson.StartTime)
	if stErr != nil {
		return false
	}
	ed, edErr := time.Parse(common.TimeModel, msgC2SJson.EndTime)
	if edErr != nil {
		return false
	}
	if ed.Unix() < st.Unix() {
		return false
	}

	return true
}

func (this *ReportController) GetPcInfo() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			fallthrough
		case common.ACCESS_REPORT:
			{
				msgEscape := this.escapeC2SString()
				bcheck := this.checkMsgC2SJson(msgEscape)
				if bcheck {
					s2cmsg, _ := Echart.CreateS2CMsg(&msgEscape, "pc_status")
					RecordUserAction(userName, accessInt, ip, "GetPcInfo成功")
					this.Data["json"] = s2cmsg
					this.ServeJSON()
				} else {
					RecordUserAction(userName, accessInt, ip, "GetPcInfo失败,发送的时间有问题")
					this.CustomAbort(http.StatusForbidden, "GetPcInfo失败,发送的时间有问题")
				}

			}
		default:
			{
				RecordUserAction(userName, accessInt, ip, "GetPcInfo失败,无此用户")
				this.CustomAbort(http.StatusUnauthorized, "GetPcInfo失败,无此用户")
			}
		}

	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

func (this *ReportController) Finish() {
	requestbody := this.Ctx.Request.Body
	if requestbody != nil {
		defer requestbody.Close()
	}
}
